﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InternshipManagement.Models;
using InternshipManagement.Helpers;

namespace InternshipManagement.Controllers
{
    [CustomAuthorize]
    public class SemesterController : Controller
    {
        SemesterRepository semesterRepository = new SemesterRepository();
        //
        // GET: /Semester/
        [CustomAuthorize(Privileges = UserPrivileges.ListSemesters)]
        public ActionResult List(int? page)
        {
            const int pageSize = 10;

            IQueryable<Semester> semesters = null;

            semesters = semesterRepository.FindAllActiveSemesters();

            var paginatedSemesters = new PaginatedList<Semester>(semesters, page ?? 0, pageSize);

            return View(paginatedSemesters);
        }
        [CustomAuthorize(Privileges = UserPrivileges.CreateSemester)]
        public ActionResult Create()
        {
            ViewBag.Op = "Create";
            return View("CreateEdit");
        }

        [HttpPost]
        [CustomAuthorize(Privileges = UserPrivileges.CreateSemester)]
        public ActionResult Create(Semester semester)
        {

            if (ModelState.IsValid)
            {
                if (!semesterRepository.SemesterExists(semester.Season, semester.Year.Value))
                {
                    semester.Status = "A";
                    semesterRepository.AddSemester(semester);
                    semesterRepository.Save();

                    return RedirectToAction("List");
                }
                else
                {
                    ModelState.AddModelError("", "The semester already exists.");
                }
            }
            ViewBag.Op = "Create";
            return View("CreateEdit", semester);
        }

        [CustomAuthorize(Privileges = UserPrivileges.EditSemester)]
        public ActionResult Edit(int id)
        {
            Semester semester = semesterRepository.GetSemester(id);
            if (semester == null)
            {
                return View("ShowMessage", "Semester does not exists");
            }
            ViewBag.Op = "Edit";
            return View("CreateEdit", semester);
        }

        [HttpPost]
        [CustomAuthorize(Privileges = UserPrivileges.EditSemester)]
        public ActionResult Edit(int id, FormCollection formvalues)
        {
            Semester semester = semesterRepository.GetSemester(id);
            if (semester == null)
            {
                return View("ShowMessage", "Semester does not exists");
            }
            if (TryUpdateModel(semester, formvalues))
            {
                if (!semesterRepository.SemesterExists(formvalues["Season"], int.Parse(formvalues["Year"])))
                {
                    semesterRepository.Save();
                    return RedirectToAction("List");
                }
                else
                {
                    ModelState.AddModelError("", "The semester already exists.");
                }
            }
            ViewBag.Op = "Edit";
            return View("CreateEdit", semester);
        }

        [CustomAuthorize(Privileges = UserPrivileges.DeleteSemester)]
        public ActionResult Delete(int id)
        {
            Semester semester = semesterRepository.GetSemester(id);
            semester.Status = "P";
            semesterRepository.Save();
            return RedirectToAction("List");
        }

        [CustomAuthorize(Privileges = UserPrivileges.SetActiveSemester)]
        public ActionResult Active(int id)
        {
            Semester active = semesterRepository.GetActiveSemester();
            active.Status = "A";
            Semester semester = semesterRepository.GetSemester(id);
            semester.Status = "C";
            semesterRepository.Save();
            return RedirectToAction("List");
        }
    }
}
